# -*- coding: utf-8 -*-
# @Time    : 2021/8/31 11:53
# @Author  : BrandonGT
# @FileName: 06_PyQt5_QPushButton.py
# 1. 导入包和模块
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
import sys

class Window(QWidget):
    def contextMenuEvent(self, QContextMenuEvent):
        print(1)
        # 创建根菜单
        menu = QMenu(self)

        # 创建子菜单
        sub_menu = QMenu(menu)
        sub_menu.setTitle("最近打开")

        # 创建QAction
        action1 = QAction("新建", menu)
        action2 = QAction("打开", menu)
        action3 = QAction("退出", menu)

        file_action = QAction("what is this?", sub_menu)

        # 设置QAction的功能
        action3.triggered.connect(window.close)

        # 将创建的QAction添加至根目录
        menu.addAction(action1)
        menu.addAction(action2)
        sub_menu.addAction(file_action)
        menu.addMenu(sub_menu)
        menu.addSeparator()
        menu.addAction(action3)

        menu.exec_(QContextMenuEvent.globalPos())



# 2. 创建一个应用程序对象
app = QApplication(sys.argv)

# 3. 创建控件
window = Window()

# 3.1 设置控件
window.setWindowTitle("")
window.resize(500, 500)

# ------------------QPushButton ------------------ 开始
btn = QPushButton(window)
btn.resize(100, 50)
btn.setText("File")
# btn.setFlat(True)

# 创建根菜单
menu = QMenu()

# 创建子菜单
sub_menu = QMenu(menu)
sub_menu.setTitle("最近打开")


# 创建QAction
action1 = QAction("新建", menu)
action2 = QAction("打开", menu)
action3 = QAction("退出", menu)

file_action = QAction("what is this?", sub_menu)

# 设置QAction的功能
action3.triggered.connect(window.close)

# 将创建的QAction添加至根目录
menu.addAction(action1)
menu.addAction(action2)
sub_menu.addAction(file_action)
menu.addMenu(sub_menu)
menu.addSeparator()
menu.addAction(action3)

# 设置menu
print(menu.isTearOffMenuVisible())

# 设置按钮的目录
btn.setMenu(menu)
btn.setDefault(True)

window.setContextMenuPolicy(Qt.CustomContextMenu)
window.customContextMenuRequested.connect(lambda : print(213123))

# ------------------QPushButton ------------------ 结束



# 3.2 展示控件
window.show()
# btn.showMenu()

# 4. 应用程序执行，进入消息循环
sys.exit(app.exec_())